package com.wb.mas.binding.banner;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.wb.mas.R;
import com.wb.mas.entity.BannerEntity;

import java.util.List;

import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.widget.banner.MZBannerView;
import me.goldze.mvvmhabit.widget.banner.holder.MZHolderCreator;
import me.goldze.mvvmhabit.widget.banner.holder.MZViewHolder;

/**
 * Copyright (C) 2018,
 * ViewAdapter
 * <p>
 * Description
 * 引导页面viewPager的适配器
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019-04-28, yue, Create file
 */
public class ViewAdapter {

    @BindingAdapter(value = {"itemData", "onGoClick"}, requireAll = false)
    public static void onInitDatasCommand(final MZBannerView mzBannerView,
                                          final List<BannerEntity> items,
                                          final BindingCommand onGoClick) {

        if (items != null) {
            mzBannerView.setPages(items, new MZHolderCreator<ViewPagerHolder>() {
                @Override
                public ViewPagerHolder createViewHolder() {
                    return new ViewPagerHolder();
                }
            });

            mzBannerView.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
                @Override
                public void onPageClick(View view, int position) {
                    onGoClick.execute(position);
                }
            });
        }
    }


    public static final class ViewPagerHolder implements MZViewHolder<BannerEntity> {
        private ImageView mPic;

        public ViewPagerHolder() {

        }

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.recycle_banner, null);
            mPic = view.findViewById(R.id.iv_pic);
            return view;
        }

        @Override
        public void onBind(Context context, int position, BannerEntity data) {
            //使用Glide框架加载图片
            Glide.with(context)
                    .load(data.getImgUrl())
                    .into(mPic);
        }
    }
}
